<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>商品列表页面</h1>
    <table border="1">
        <tr>
            <th>id</th>
            <th>商品标题</th>
            <th>商品价格</th>
            <th>商品库存</th>
            <th>操作</th>
        </tr>
        <tr v-for="p in arr">
            <td>{{p.id}}</td>
            <td>{{p.title}}</td>
            <td>{{p.price}}</td>
            <td>{{p.num}}</td>
            <td><a :href="'/update.html?id='+p.id">修改</a>
                <!--如果点击超链接不离开页面的话,需要将超链接跳转功能取消掉
            href="javascript:void(0)"-->
                <a href="javascript:void(0)" @click="del(p.id)">删除</a></td>

        </tr>
    </table>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            // arr:[{id:1,title:"苹果",price:5,num:100},
            // {id:2,title:"梨子",price:5,num:100},
            // {id:3,title:"香蕉",price:5,num:100},
            // {id:3,title:"猕猴桃",price:5,num:100}]
            arr: []
        },
        methods: {
            del(id) {
                if (confirm("您确认删除此商品嘛")) {
                    axios.get("/delete?id=" + id).then(function (response) {
                        location.reload();
                    })
                }
            }
        },
        //created方法是Vue对象中内置的方法,当Vue对象初始化时会调用此方法
        //通常情况下吧进入页面后立即发出请求的代码写在此处
        created: function () {
            console.log("代码执行了")
            //发出异步请求获取数据
            axios.get("/select").then(function (response) {
                //这里得到的服务器响应的数据是一个数组里面装着多个商品对象
                v.arr = response.data;
            })
        }
    })
</script>
</html>